package rs.networx.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.CommentDao;
//import ftn.mbs2.dao.NottificationCategory;
import rs.networx.dao.NottificationDao;
import rs.networx.dao.PersonDao;
import rs.networx.dao.PostDao;
import rs.networx.entity.Comment;
import rs.networx.entity.Nottification;
import rs.networx.entity.Person;
import rs.networx.entity.Post;

public class AddCommentServlet extends HttpServlet {

	private static final long serialVersionUID = 7441232237003136002L;

	@EJB
	private PersonDao personDao;

	@EJB
	private NottificationDao nottificationDao;

	@EJB
	PostDao postDao; 
	
	@EJB
	CommentDao commentDao; 

	private String username;
	private String password;
	private Integer intLoggedUserID;
	private Person loggedUser;
	
	private String commentText;
	private Comment comment;
	
	private String postID;
	private Integer intPostID;
	private Post post;
	
	private Person postOwner;
	private Person postPoster;
	
	private Integer nottificationsCount;
	
	private Set<Person> loggedUsersFriendsSet;
	private List<Person> loggedUsersFriendsList;

	private List<Post> loggedUsersOwnedPosts;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		System.out.println("LOADED: CommentOnPostLoggedUserWallServlet");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		commentText = request.getParameter("commentText");
		postID = request.getParameter("postID");

		try {	
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			intLoggedUserID = loggedUser.getId();
			System.out.println(username + "  " + password + "  " + intLoggedUserID);
			
			intPostID = Integer.parseInt(postID);
			System.out.println(commentText + "  " + postID + "  " + intPostID);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		
		// ako je korisnik uneo prazan komentar, tada ga ne upisujemo u bazu i vracamo ga na stranicu na kojoj je bio...
		if(commentText == "" && postID != ""){
			response.sendRedirect("GoBackToRegistredUserHomePageServlet");
			return;	
		}
		
		if(loggedUser != null){	
			// creating new comment...
			comment = new Comment();
			comment.setText(commentText);
			comment.setDate(Calendar.getInstance().getTime());

			post = postDao.findById(intPostID);
			comment.setPost(post);
			
			postOwner = post.getOwner();
			postPoster = post.getPoster();
			
			System.out.println("postOwner: " + postOwner);
			System.out.println("postPoster: " + postPoster);
			
			System.out.println("loggedUserID: " + intLoggedUserID);
			System.out.println("postOwnerID: " + postOwner.getId());
			System.out.println("postPosterID: " + postPoster.getId());
			
			if(intLoggedUserID.equals(postPoster.getId()) && intLoggedUserID.equals(postOwner.getId()) &&
					postPoster.getId().equals(postOwner.getId())){
				// znaci da je logovani user komentarisao na svoj post...
				comment.setCommetator(loggedUser);
				
				// treba jos dodati i notifikaciju da je dati loggedd user komentarisao na svoj post...
				// adding nottification to logged user...
				Nottification nottification = new Nottification();
				nottification.setOwner(loggedUser);
				nottification.setTargetID(post.getId());
				nottification.setType(3);
				nottification.setViewed(false);
				nottification.setLink("You Commented On Your Own Post: " + 
									  "<i style=" + "\"" + "color: blue" + "\"" + ">" + post.getTitle() + "</i>" + 
									  " On Your Wall" + ".");
				nottificationDao.persist(nottification);
				
			}else if(!intLoggedUserID.equals(postPoster.getId()) && intLoggedUserID.equals(postOwner.getId()) && 
					!postOwner.getId().equals(postPoster.getId())){
				// znaci da je neko drugi okacio post na logged users wall(sto znaci da su prijatelji)...
				comment.setCommetator(loggedUser);
				
				// treba jos dodati i notifikaciju da je dati loggedd user komentarisao na post koji je okacio postPoster...
				// adding nottification to logged user...
				Nottification nottification = new Nottification();
				nottification.setOwner(loggedUser);
				nottification.setTargetID(post.getId());
				nottification.setType(3);
				nottification.setViewed(false);
				nottification.setLink("You Commented On " +
						  			  "<a href=\"GoToSelectedPersonProfileServlet2?personID=" + postPoster.getId() + "\"" + 
						  			  " target=\"_blank\"" + ">" + postPoster.getFirstName() + " " + postPoster.getLastName() + "</a>" + "'s" + 
						  			  " Post: " + 
						  			  "<i style=" + "\"" + "color: blue" + "\"" + ">" + post.getTitle() + "</i>" + 
						  			  " On Your Wall" + ".");
				nottificationDao.persist(nottification);
			}
			
			//commentDao.addComment(comment);
			commentDao.persist(comment);
			System.out.println(comment);
			
			// sada bi trebalo dodati i notifikaciju za svakog prijatelja logged usera da je logged user komentarisao na svoj post...
			loggedUsersFriendsSet = personDao.getFriends(intLoggedUserID);
			System.out.println(loggedUsersFriendsSet.size());
			
			if(loggedUsersFriendsSet == null || loggedUsersFriendsSet.size() == 0){
				// znaci da nema prijatelja, pa idemo dalje...
				System.out.println("loggedUsersFriendsSet: " + loggedUsersFriendsSet.size());
				
			}else if(loggedUsersFriendsSet != null && loggedUsersFriendsSet.size() > 0){
				loggedUsersFriendsList = new ArrayList<Person>();
				
				Iterator<Person> iter = loggedUsersFriendsSet.iterator();
			    while(iter.hasNext()){
			    	loggedUsersFriendsList.add((Person)iter.next());
			    }
			    System.out.println(loggedUsersFriendsList.size());
			    
			    // sada treba za svakog prijatelja da napravimo po notifikaciju...
				for(int i = 0; i < loggedUsersFriendsList.size(); i++){
					
					if(intLoggedUserID.equals(postPoster.getId()) && intLoggedUserID.equals(postOwner.getId()) &&
							postPoster.getId().equals(postOwner.getId())){
						// znaci da je logovani user komentarisao na svoj post...
						
						// adding nottification to logged users friends...
						Nottification nottification2 = new Nottification();
						nottification2.setOwner(loggedUsersFriendsList.get(i));
						nottification2.setTargetID(post.getId());
						nottification2.setType(3);
						nottification2.setViewed(false);
						nottification2.setLink("<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
					   			  			   " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + 
					   			  			   " Commented On " +
					   			  			   "<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
		   			  			  			   " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + "'s" + 
					   			  			   " Post: " + 
					   			  			   "<i style=" + "\"" + "color: blue" + "\"" + ">" + post.getTitle() + "</i>" + 
					   			  			   " On " + 
					   			  			   "<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
		   			  			  			   " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + "'s" +
					   			  			   " Wall.");
						nottificationDao.persist(nottification2);
						
					}else if(!intLoggedUserID.equals(postPoster.getId()) && intLoggedUserID.equals(postOwner.getId()) &&
							!postOwner.getId().equals(postPoster.getId())){
						// znaci da je neko drugi okacio post na logged users wall koji je on(logged user) komentarisao...
						
						// adding nottification to logged users friends...
						Nottification nottification2 = new Nottification();
						nottification2.setOwner(loggedUsersFriendsList.get(i));
						nottification2.setTargetID(post.getId());
						nottification2.setType(3);
						nottification2.setViewed(false);
						nottification2.setLink("<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
		   			  			  			   " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + 
		   			  			  			   " Commented On " +
		   			  			  			   "<a href=\"GoToSelectedPersonProfileServlet2?personID=" + postPoster.getId() + "\"" + 
		   			  			  			   " target=\"_blank\"" + ">" + postPoster.getFirstName() + " " + postPoster.getLastName() + "</a>" + "'s" + 
		   			  			  			   " Post: " + 
		   			  			  		       "<i style=" + "\"" + "color: blue" + "\"" + ">" + post.getTitle() + "</i>" +
		   			  			  		       " On " + 
		   			  			  		       "<a href=\"GoToSelectedPersonProfileServlet2?personID=" + loggedUser.getId() + "\"" + 
		   			  			  		       " target=\"_blank\"" + ">" + loggedUser.getFirstName() + " " + loggedUser.getLastName() + "</a>" + "'s" +
				   			  			   	   " Wall.");
						nottificationDao.persist(nottification2);
					}
				}
			}
			
			// dobavljamo postove za logovanog usera...
			loggedUsersOwnedPosts = personDao.getFirstTenOwnedPosts(loggedUser.getId());
			System.out.println("loggedUsersOwnedPosts: " + loggedUsersOwnedPosts.size());
			
			// sada dobavljamo i notifikacije za datog ulogovanog korisnika ako ih ima...
			nottificationsCount = nottificationDao.getNottificationsCountForLoggedUser(loggedUser);
			System.out.println("nottificationsCount: " + nottificationsCount);
			
			session.setAttribute("loggedUsersOwnedPosts", loggedUsersOwnedPosts);
			session.setAttribute("nottificationsCount", nottificationsCount);
			
			response.sendRedirect("ViewWallServlet");
			return;	
		}else if(loggedUser == null){
			request.getRequestDispatcher("Welcome.jsp").forward(request, response);
			return;
		}		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	
	
}
